<?php
class ModelMartCrmcustomerrfidreport extends Model {
	public function getCustomersRFID($data = array()) { 
		$sql = "SELECT crm_goods.id as id_group_3, crm_goods.name, SUM( crm_customer_transactions.amount ) AS amount
                FROM crm_goods, crm_customer_transactions, crm_customer_transactions_crm_goods_c, crm_customer_bills, crm_customer_transactions_crm_customer_bills_c, crm_industry, crm_goods_crm_industry_c, crm_industry_crm_sec_industry_c
                WHERE 
                crm_goods.id = crm_goods_crm_industry_c.crm_goods_crm_industrycrm_goods_idb
                AND
                crm_industry.id = crm_goods_crm_industry_c.crm_goods_crm_industrycrm_industry_ida
                AND
                crm_goods.id = crm_customer_transactions_crm_goods_c.crm_customer_transactions_crm_goodscrm_goods_ida
                AND crm_customer_transactions.id = crm_customer_transactions_crm_goods_c.crm_customer_transactions_crm_goodscrm_customer_transactions_idb
                AND crm_customer_transactions.id = crm_customer_transactions_crm_customer_bills_c.crm_customaf9eactions_idb
                AND crm_customer_bills.id = crm_customer_transactions_crm_customer_bills_c.crm_custom19b9r_bills_ida

                AND
                crm_industry.id = crm_industry_crm_sec_industry_c.crm_industry_crm_sec_industrycrm_industry_idb
                AND
                crm_goods.id = crm_goods_crm_industry_c.crm_goods_crm_industrycrm_goods_idb ";
                if (!empty($data['id_group_2'])) {
                $sql .= " AND crm_industry_crm_sec_industry_c.crm_industry_crm_sec_industrycrm_sec_industry_ida = '". $this->db->escape($data['id_group_2'])."' ";
            }
            if (!empty($data['id_group_3'])) {
                $sql .= " AND crm_goods_crm_industry_c.crm_goods_crm_industrycrm_industry_ida = '". $this->db->escape($data['id_group_3'])."' ";
            }
            if (!empty($data['filter_date_start'])) {
			$sql .= " AND DATE(crm_customer_bills.bill_date) >= '" . $this->db->escape($data['filter_date_start']) . "' ";
		}

		if (!empty($data['filter_date_end'])) {
			$sql .= " AND DATE(crm_customer_bills.bill_date) <= '" . $this->db->escape($data['filter_date_end']) . "' ";
		}
                $sql .= " GROUP BY crm_goods.id
                ORDER BY SUM( crm_customer_transactions.amount ) DESC 
                ";
		//$sql = "select * from accounts";	
                //echo $sql;
		$query = $this->db->query($sql);
		return $query->rows;
                echo $query;
	}

	public function getTotalCustomersOnline($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "customer_online` co LEFT JOIN " . DB_PREFIX . "customer c ON (co.customer_id = c.customer_id)";
		
		$implode = array();
		
		if (isset($data['filter_ip']) && !is_null($data['filter_ip'])) {
			$implode[] = "co.ip LIKE '" . $this->db->escape($data['filter_ip']) . "'";
		}
		
		if (isset($data['filter_customer']) && !is_null($data['filter_customer'])) {
			$implode[] = "co.customer_id > 0 AND CONCAT(c.firstname, ' ', c.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "'";
		}
		
		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}
				
		$query = $this->db->query($sql);

		return $query->row['total'];
	}
}
?>